package com.zhiwang.planet.ui.my.record

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import com.zhiwang.planet.R
import com.zhiwang.planet.util.DiffCallBack
import com.zhiwang.planet.util.dp
import com.zhiwang.planet.widget.TimeLineTextView

abstract class CommonItemData(/*var title: String, var date: String, var content: String*/) {

    abstract fun getItemTitle(): String

    abstract fun getItemDate(): String

    abstract fun getItemContent(): String

    open fun time() = ""

    open fun date() = ""
}

class CommonRecordAdapter constructor(diff: DiffUtil.ItemCallback<CommonItemData>) : PagedListAdapter<CommonItemData, CommonRecordAdapter.ItemViewHolder>(diff) {

//    var datas = ArrayList<CommonItemData>()

    var colorAdd: Int = 0
    var colorMinus: Int = 0


    var resId: Int = 0
    fun setTypeIconResource(resId: Int) {
        this.resId = resId
        notifyDataSetChanged()
    }

//    fun setDatas(datas: List<CommonItemData>) {
//        if (datas.isEmpty())
//            return
//        val prev = this.datas.size
//        this.datas.addAll(datas)
//        notifyItemRangeInserted(prev, this.datas.size - 1)
//    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        if (colorAdd == 0) colorAdd = ContextCompat.getColor(parent.context, R.color.colorOrange)
        if (colorMinus == 0) colorMinus = ContextCompat.getColor(parent.context, R.color.colorGreen)
        val view = layoutInflater.inflate(R.layout.item_diamond_detail, parent, false)
        return ItemViewHolder(view)
    }

//    override fun getItemCount(): Int {
//
//        return datas.size
//    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        getItem(holder.adapterPosition)?.apply { holder.bind(this) }
    }

//    fun clear() {
//        this.datas.clear()
//        notifyDataSetChanged()
//    }

    inner class ItemViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView) {

        var title: TimeLineTextView
        var date: TextView
        var time: TextView
        var content: TextView

        var icon: ImageView

        fun bind(commonItemData: CommonItemData) {
            title.headerVisible = adapterPosition != 0
            if (adapterPosition == itemCount - 1) title.bottomExtra = 60.dp
            else title.bottomExtra = 30.dp
//            val commonItemData = getItem(adapterPosition)!!
            title.text = commonItemData.getItemTitle()
            title.isSelected = true
            date.text = commonItemData.date()
            time.text = commonItemData.time()
            content.text = commonItemData.getItemContent()
            if (resId != 0)
                icon.setImageResource(resId)
            try {
                content.setTextColor(if (commonItemData.getItemContent().toInt() >= 0) colorAdd else colorMinus)
            } catch (e: Exception) {
                content.setTextColor(colorAdd)
            }

        }

        init {
            title = itemView.findViewById(R.id.title)
            date = itemView.findViewById(R.id.date)
            time = itemView.findViewById(R.id.time)
            content = itemView.findViewById(R.id.reward_num)
            icon = itemView.findViewById(R.id.iv_type_icon)
        }

    }
}